//
//  GuiTable.cpp
//  SpaceTrader
//
//  Created by Brandon Chastain on 10/22/12.
//  Copyright (c) 2012 The Hex Pistols. All rights reserved.
//

#include "GuiTools.h"
#include "StateManager.h"

GuiTable::GuiTable(int rows, int cols, int x, int y, int width, int height, int fontSize) : rows(rows),
                        cols(cols), x(x), y(y), w(width), h(height),
                        table(*new std::vector<std::vector<SDL_Surface*> >(cols)), stdFont(TTF_OpenFont("Futura.ttc", fontSize)),
                        white(SDL_Color())
{
    for (unsigned int i = 0; i < table.size(); i++){
        table[i] = *new std::vector<SDL_Surface*>(rows);
    }
    white.r = 255;
    white.g = 255;
    white.b = 255;
}

GuiTable::GuiTable(const GuiTable &G) : rows(G.rows),
cols(G.cols), x(G.x), y(G.y), w(G.w), h(G.h), table(*new std::vector<std::vector<SDL_Surface*> >(G.cols)), stdFont(G.stdFont), white(G.white)
{
    for (unsigned int i = 0; i < table.size(); i++){
        table[i] = std::vector<SDL_Surface*>(G.table[i].size());
        for (unsigned int j = 0; j < table[i].size(); j++){
            table[i][j] = G.table[i][j];
        }
    }
}

GuiTable::~GuiTable()
{
	for (unsigned int i = 0; i < table.size(); i ++) {
		for (unsigned int j = 0; j < table[i].size(); j++) {
			SDL_FreeSurface(table[i][j]);
		}
	}
	TTF_CloseFont(stdFont);
}

GuiTable& GuiTable::operator=(const GuiTable &G){
    if (&G != this){
        TTF_CloseFont(stdFont);
        for (unsigned int i = 0; i < table.size(); i ++) {
            for (unsigned int j = 0; j < table[i].size(); j++) {
                SDL_FreeSurface(table[i][j]);
            }
        }
        rows = G.rows;
        cols = G.cols;
        x = G.x;
        y = G.y;
        w = G.w;
        h = G.h;
        table = std::vector<std::vector<SDL_Surface*> >(G.table.size());
        for (unsigned int i = 0; i < table.size(); i ++) {
            table[i] = std::vector<SDL_Surface*>(G.table[i].size());
            for (unsigned int j = 0; j < table[i].size(); j++){
                table[i][j] = G.table[i][j];
            }
        }
        stdFont = G.stdFont;
        white = G.white;
    }
    return *this;
}

/*
 * Sets the contents of the table with the given character pointer array
 */
void GuiTable::setContents(const char* contents[])
{
	/*printf("test: %08x\n",&contents[7]);
	printf("test: %s\n",contents[7]);
	int s0 = table.size();
	int s1 = table[0].size();
	printf("test: %s\n",contents[7]);*/


    int count = 0;
    for (unsigned int i = 0; i < table.size(); i++){
        for (unsigned int j = 0; j < table[i].size(); j++){
			if (table[i][j] != NULL) free(table[i][j]);
			//printf("in the loop%s: %08x\n",contents[7], &contents[7]);

            table[i][j] = TTF_RenderText_Blended(stdFont, contents[count++], white);
        }
    }
}

/*
 * Sets the contents of the table with the given vector of strings
 */
void GuiTable::setContents(std::vector<std::string> contents)
{
    SDL_Color red = {255, 0, 0};
    int count = 0;
    for (unsigned int i = 0; i < table.size(); i++){
        for (unsigned int j = 0; j < table[i].size(); j++){
			//if (table[i][j] != NULL) free(table[i][j]);
			//printf("in the loop%s: %08x\n",contents[7], &contents[7]);
            if (contents[count].at(0) == '|'){
                if (contents[count].at(1) == 'r'){
                    table[i][j] = TTF_RenderText_Blended(stdFont, contents[count++].substr(2,contents.size()-2).c_str(), red);
                }
            } else {
                table[i][j] = TTF_RenderText_Blended(stdFont, contents[count++].c_str(), white);
            }
        }
    }
}

/*
 * Applies the table values to the screen
 */
void GuiTable::show(SDL_Surface *screen)
{
    for (unsigned int i = 0; i < table.size(); i++){
        for (unsigned int j = 0; j < table[i].size(); j++){
            apply_surface(x+i*w, y+j*h, table[i][j], screen);
        }
    }
}

/*
 * Sets the coordinates to draw this table to on the screen
 */
void GuiTable::setCoords(int x, int y)
{
    this->x = x;
    this->y = y;
}

/*
 * Sets the x value of the table for drawing purposes
 */
void GuiTable::setX(int x)
{
	this->x = x;
}

/*
 * Sets the y value of the table for drawing purposes
 */
void GuiTable::setY(int y)
{
	this->y = y;
}

/*
 * Returns the total width of the table
 */
int GuiTable::getTotalWidth()
{
    int max = 0;
    for (unsigned int j = 0; j < table[cols-1].size(); j++){
        if (table[cols-1][j]->w > max){
            max = table[cols-1][j]->w;
        }
    }
    return max+w*(cols-1);
}